var req;
var stat;
var result;

function listrefresh(ip) {
    var url = "http://localhost:8080/list?ip=" + ip;
    stat = ip;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    req.send(null);
}

function callback() {
    if (req.readyState == 4 && req.status == 200) {
        var res = req.responseText;
        show(res);
    }
}

function show(str) {
    var data = str.split(",");
    if(stat === -1){
        var i,j = 1;
        for(i = 8; i >= 1; i--){
            if(data[i * 2 - 1] !== "null" && data[i * 2] !== "null"){
                document.getElementById("ip" + j).innerText = data[i * 2 - 1];
                document.getElementById("prize" + j).innerText = data[i * 2];
                j++;
            }
        }
    }
    else{
        result = str[0];
        display();
        listrefresh(-1);
    }
}
setInterval("listrefresh(-1)", 5000);